function transposeArray(array) {
  return array[0].map((col, i) => array.map(row => row[i]));
}

export function tranStr(str) {
  return str == undefined || str == '' || Object.is(str, NaN) ? 0 : Number.parseInt(str);
}

export function disposeAls(als) {
  // als = {
  //   "d7": {},
  //   "m1": {
  //     "id": {
  //       "nbank": {
  //         "cf_orgnum": "1",
  //         "cons_allnum": "1",
  //         "week_allnum": "0",
  //         "selfnum": "0",
  //         "orgnum": "1",
  //         "night_allnum": "0",
  //         "cf_allnum": "1",
  //         "cons_orgnum": "1",
  //         "week_orgnum": "0",
  //         "night_orgnum": "0",
  //         "allnum": "1"
  //       },
  //       "pdl": {
  //         "orgnum": "1",
  //         "allnum": "1"
  //       }
  //     },
  //     "cell": {
  //       "nbank": {
  //         "cf_orgnum": "1",
  //         "cons_allnum": "1",
  //         "week_allnum": "0",
  //         "selfnum": "0",
  //         "orgnum": "1",
  //         "night_allnum": "0",
  //         "cf_allnum": "1",
  //         "cons_orgnum": "1",
  //         "week_orgnum": "0",
  //         "night_orgnum": "0",
  //         "allnum": "1"
  //       },
  //       "pdl": {
  //         "orgnum": "1",
  //         "allnum": "1"
  //       }
  //     }
  //   },
  //   "m3": {
  //     "id": {
  //       "tot_mons": "2",
  //       "max_monnum": "1",
  //       "min_inteday": "50",
  //       "nbank": {
  //         "cons_allnum": "2",
  //         "cf_orgnum": "1",
  //         "orgnum": "1",
  //         "max_inteday": "50",
  //         "cf_allnum": "2",
  //         "allnum": "2",
  //         "week_allnum": "0",
  //         "tot_mons": "2",
  //         "selfnum": "0",
  //         "night_allnum": "0",
  //         "max_monnum": "1",
  //         "min_inteday": "50",
  //         "cons_orgnum": "1",
  //         "week_orgnum": "0",
  //         "night_orgnum": "0",
  //         "avg_monnum": "1.00",
  //         "min_monnum": "0"
  //       },
  //       "max_inteday": "50",
  //       "pdl": {
  //         "orgnum": "1",
  //         "allnum": "2"
  //       },
  //       "avg_monnum": "1.00",
  //       "min_monnum": "0"
  //     },
  //     "cell": {
  //       "tot_mons": "2",
  //       "max_monnum": "1",
  //       "min_inteday": "50",
  //       "nbank": {
  //         "cons_allnum": "2",
  //         "cf_orgnum": "1",
  //         "orgnum": "1",
  //         "max_inteday": "50",
  //         "cf_allnum": "2",
  //         "allnum": "2",
  //         "week_allnum": "0",
  //         "tot_mons": "2",
  //         "selfnum": "0",
  //         "night_allnum": "0",
  //         "max_monnum": "1",
  //         "min_inteday": "50",
  //         "cons_orgnum": "1",
  //         "week_orgnum": "0",
  //         "night_orgnum": "0",
  //         "avg_monnum": "1.00",
  //         "min_monnum": "0"
  //       },
  //       "max_inteday": "50",
  //       "pdl": {
  //         "orgnum": "1",
  //         "allnum": "2"
  //       },
  //       "avg_monnum": "1.00",
  //       "min_monnum": "0"
  //     }
  //   },
  //   "fst": {
  //     "id": {
  //       "nbank": {
  //         "inteday": "316"
  //       }
  //     },
  //     "cell": {
  //       "nbank": {
  //         "inteday": "316"
  //       }
  //     }
  //   },
  //   "d15": {},
  //   "m6": {},
  //   "lst": {
  //     "id": {
  //       "nbank": {
  //         "inteday": "27",
  //         "consnum": "1",
  //         "csinteday": "1"
  //       }
  //     },
  //     "cell": {
  //       "nbank": {
  //         "inteday": "27",
  //         "consnum": "1",
  //         "csinteday": "1"
  //       }
  //     }
  //   },
  //   "m12": {
  //     "id": {
  //       "tot_mons": "7",
  //       "max_monnum": "1",
  //       "min_inteday": "43",
  //       "nbank": {
  //         "cons_allnum": "7",
  //         "cf_orgnum": "1",
  //         "orgnum": "1",
  //         "max_inteday": "52",
  //         "cf_allnum": "7",
  //         "allnum": "7",
  //         "week_allnum": "1",
  //         "tot_mons": "7",
  //         "selfnum": "0",
  //         "night_allnum": "0",
  //         "max_monnum": "1",
  //         "min_inteday": "43",
  //         "cons_orgnum": "1",
  //         "week_orgnum": "1",
  //         "night_orgnum": "0",
  //         "avg_monnum": "1.00",
  //         "min_monnum": "0"
  //       },
  //       "max_inteday": "52",
  //       "pdl": {
  //         "orgnum": "1",
  //         "allnum": "7"
  //       },
  //       "avg_monnum": "1.00",
  //       "min_monnum": "0"
  //     },
  //     "cell": {
  //       "tot_mons": "7",
  //       "max_monnum": "1",
  //       "min_inteday": "43",
  //       "nbank": {
  //         "cons_allnum": "7",
  //         "cf_orgnum": "1",
  //         "orgnum": "1",
  //         "max_inteday": "52",
  //         "cf_allnum": "7",
  //         "allnum": "7",
  //         "week_allnum": "1",
  //         "tot_mons": "7",
  //         "selfnum": "0",
  //         "night_allnum": "0",
  //         "max_monnum": "1",
  //         "min_inteday": "43",
  //         "cons_orgnum": "1",
  //         "week_orgnum": "1",
  //         "night_orgnum": "0",
  //         "avg_monnum": "1.00",
  //         "min_monnum": "0"
  //       },
  //       "max_inteday": "52",
  //       "pdl": {
  //         "orgnum": "1",
  //         "allnum": "7"
  //       },
  //       "avg_monnum": "1.00",
  //       "min_monnum": "0"
  //     }
  //   }
  // }

  const yhjg_params = [
    '',
    'tra_',
    'ret_',
    'week_',
    'night_'
  ]

  const fyhjg_params = [
    '',
    'week_',
    'night_',
    'mc_',
    'cf_',
    'ca_',
    'else_',
    'p2p_',
    'com_',
    'sloan_',
    'cons_',
    'nsloan_',
    'finlea_',
    'autofin_'
  ]

  const fljd_params = [
    'rel',
    'cooff',
    'caoff',
    'pdl',
    'coon',
    'caon',
    'af',
    'oth'
  ]

  const pub_params = [
    'min_monnum',
    'max_monnum',
    'tot_mons',
    'min_inteday',
    'max_inteday',
    'avg_monnum'
  ]
  const myzhtj = ['次', '次', '个月', '天', '天', '次',]

  let yhjg = []
  let fyhjg = []
  let fljd = []
  let yftj = []
  let time_crecle = ['d7', 'd15', 'm1', 'm3', 'm6', 'm12']

  for (let i = 0; i < time_crecle.length; i++) {
    const tc = time_crecle[i]
    if (Object.keys(als[tc]).length == 0) {
      yhjg.push(['0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家'])
      fyhjg.push(['0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家'])
      fljd.push(['0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家'])
      if (i > 2) {
        yftj.push(['0次 | 0次 | 0次', '0次 | 0次 | 0次', '0月 | 0月 | 0月', '0天 | 0天 | 0天', '0天 | 0天 | 0天', '0次 | 0次 | 0次'])
      }
    } else {
      const alsItem = als[tc]
      // 银行处理
      const idBank = alsItem.id != undefined ? alsItem.id.bank != undefined ? alsItem.id.bank : null : null
      const cellBank = alsItem.cell != undefined ? alsItem.cell.bank != undefined ? alsItem.cell.bank : null : null
      let idOrCell = []
      if (idBank != null && cellBank != null) {
        for (let j = 0; j < yhjg_params.length; j++) {
          let item = yhjg_params[j]
          let allnum = tranStr(idBank[item + 'allnum']) + tranStr(cellBank[item + 'allnum'])
          let orgnum = tranStr(idBank[item + 'orgnum']) + tranStr(cellBank[item + 'orgnum'])
          idOrCell.push(allnum + '次 | ' + orgnum + '家')
        }
      } else if (idBank != null && cellBank == null) {
        for (let j = 0; j < yhjg_params.length; j++) {
          let item = yhjg_params[j]
          let allnum = tranStr(idBank[item + 'allnum'])
          let orgnum = tranStr(idBank[item + 'orgnum'])
          idOrCell.push(allnum + '次 | ' + orgnum + '家')
        }
      } else if (idBank == null && cellBank != null) {
        for (let j = 0; j < yhjg_params.length; j++) {
          let item = yhjg_params[j]
          let allnum = tranStr(cellBank[item + 'allnum'])
          let orgnum = tranStr(cellBank[item + 'orgnum'])
          idOrCell.push(allnum + '次 | ' + orgnum + '家')
        }
      } else {
        idOrCell = ['0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家']
      }
      yhjg.push(idOrCell)

      // 非银处理
      const idNBank = alsItem.id != undefined ? alsItem.id.nbank != undefined ? alsItem.id.nbank : null : null
      const cellNBank = alsItem.cell != undefined ? alsItem.cell.nbank != undefined ? alsItem.cell.nbank : null : null
      idOrCell = []
      if (idNBank != null && cellNBank != null) {
        for (let j = 0; j < fyhjg_params.length; j++) {
          let item = fyhjg_params[j]
          let allnum = tranStr(idNBank[item + 'allnum']) + tranStr(cellNBank[item + 'allnum'])
          let orgnum = tranStr(idNBank[item + 'orgnum']) + tranStr(cellNBank[item + 'orgnum'])
          idOrCell.push(allnum + '次 | ' + orgnum + '家')
        }
      } else if (idNBank != null && cellNBank == null) {
        for (let j = 0; j < fyhjg_params.length; j++) {
          let allnum = tranStr(idNBank[fyhjg_params[j] + 'allnum'])
          let orgnum = tranStr(idNBank[fyhjg_params[j] + 'orgnum'])
          idOrCell.push(allnum + '次 | ' + orgnum + '家')
        }
      } else if (idNBank == null && cellNBank != null) {
        for (let j = 0; j < fyhjg_params.length; j++) {
          let allnum = tranStr(cellNBank[fyhjg_params[j] + 'allnum'])
          let orgnum = tranStr(cellNBank[fyhjg_params[j] + 'orgnum'])
          idOrCell.push(allnum + '次 | ' + orgnum + '家')
        }
      } else {
        idOrCell = ['0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家', '0次 | 0家']
      }
      fyhjg.push(idOrCell)

      // 分类借贷统计
      const tjItem = []
      for (let j = 0; j < fljd_params.length; j++) {
        let fp = fljd_params[j]
        let id = alsItem.id != undefined ? alsItem.id[fp] != undefined ? alsItem.id[fp] : 0 : 0
        let cell = alsItem.cell != undefined ? alsItem.cell[fp] != undefined ? alsItem.cell[fp] : 0 : 0
        if (id != 0 && cell != 0) {
          tjItem.push((tranStr(id.allnum) + tranStr(cell.allnum)) + '次 | ' + (tranStr(id.orgnum) + tranStr(cell.orgnum)) + '家')
        } else if (id != 0 && cell == 0) {
          tjItem.push(tranStr(id.allnum) + '次 | ' + tranStr(id.orgnum) + '家')
        } else if (id == 0 && cell != 0) {
          tjItem.push(tranStr(cell.allnum) + '次 | ' + tranStr(cell.orgnum) + '家')
        } else {
          tjItem.push('0次 | 0家')
        }
      }
      fljd.push(tjItem)

      // 月份以上统计
      if (i > 2) {
        let yftjItem = []
        for (let j = 0; j < pub_params.length; j++) {
          let fp = pub_params[j]
          // 统计
          let source_id_item = alsItem.id != undefined ? alsItem.id[fp] != undefined ? alsItem.id[fp] : 0 : 0
          let source_cell_item = alsItem.cell != undefined ? alsItem.cell[fp] != undefined ? alsItem.cell[fp] : 0 : 0
          if (j == 3) {
            yftjItem[j] = (tranStr(source_id_item) > tranStr(source_cell_item) ? tranStr(source_cell_item) : tranStr(source_id_item)) + myzhtj[j]
          } else if (j == 4) {
            yftjItem[j] = (tranStr(source_id_item) < tranStr(source_cell_item) ? tranStr(source_cell_item) : tranStr(source_id_item)) + myzhtj[j]
          } else {
            let n = tranStr(source_id_item) + tranStr(source_cell_item)
            yftjItem[j] = (j == 2 ? n / 2 : n) + myzhtj[j]
          }
          let num = 0
          // 银行
          if (idBank != null && cellBank != null) {
            if (j == 3) {
              num = (tranStr(idBank[fp]) > tranStr(cellBank[fp]) ? tranStr(cellBank[fp]) : tranStr(idBank[fp]))
            } else if (j == 4) {
              num = (tranStr(idBank[fp]) < tranStr(cellBank[fp]) ? tranStr(cellBank[fp]) : tranStr(idBank[fp]))
            } else {
              num = tranStr(idBank[fp]) + tranStr(cellBank[fp])
              num = j == 2 ? num / 2 : num
            }
          } else if (idBank != null && cellBank == null) {
            num = tranStr(idBank[fp])
          } else if (idBank == null && cellBank != null) {
            num = tranStr(cellBank[fp])
          }
          yftjItem[j] += (' | ' + num + myzhtj[j])

          // 非银
          num = 0
          // console.log(idNBank, cellNBank, fp)
          if (idNBank != null && cellNBank != null) {
            if (j == 3) {
              num = (tranStr(idNBank[fp]) > tranStr(cellNBank[fp]) ? tranStr(cellNBank[fp]) : tranStr(idNBank[fp]))
            } else if (j == 4) {
              num = (tranStr(idNBank[fp]) < tranStr(cellNBank[fp]) ? tranStr(cellNBank[fp]) : tranStr(idNBank[fp]))
            } else {
              num = tranStr(idNBank[fp]) + tranStr(cellNBank[fp])
              num = j == 2 ? num / 2 : num
            }
          } else if (idNBank != null && cellNBank == null) {
            num = tranStr(idNBank[fp])
          } else if (idNBank == null && cellNBank != null) {
            num = tranStr(cellNBank[fp])
          }
          yftjItem[j] += (' | ' + num + myzhtj[j])
        }
        yftj.push(yftjItem)
      }
    }
  }
  return {
    'yhjg': transposeArray(yhjg),
    'fyhjg': transposeArray(fyhjg),
    'fljd': transposeArray(fljd),
    'yftj': transposeArray(yftj)
  }
}

export function disposeZlAls(als) {
  let zlAls = []
  let time_crecle = ['d3', 'd7', 'd14', 'm1', 'm3', 'm6', 'm12']
  for (let i = 0; i < time_crecle.length; i++) {
    const tc = time_crecle[i]
    if (als[tc] == undefined) {
      zlAls.push([0, 0])
    } else {
      let item = als[tc]
      if (item['cell'] != undefined && item['id'] != undefined) {
        zlAls.push([tranStr(item['cell'].allnum) + tranStr(item['id'].allnum), tranStr(item['cell'].orgnum) + tranStr(item['id'].orgnum)])
      } else if (item['cell'] != undefined && item['id'] == undefined) {
        zlAls.push([tranStr(item['cell'].allnum), tranStr(item['cell'].orgnum)])
      } else if (item['cell'] == undefined && item['id'] != undefined) {
        zlAls.push([tranStr(item['id'].allnum), tranStr(item['id'].orgnum)])
      } else {
        zlAls.push([0, 0])
      }
    }
  }
  return transposeArray(zlAls)
}

export function disposeFxmd(SpecialList_c) {
  let id = SpecialList_c.id
  let cell = SpecialList_c.cell
  let fmmd = []
  // 通过id查询法院失信人命中次数
  fmmd.push(id.court_bad == '0' ? '命中（' + id.court_bad_allnum + '）' : '未命中')
  // 通过id查询被执行人命中次数
  fmmd.push(id.court_executed == '0' ? '命中（' + id.court_executed_allnum + '）' : '未命中')
  var items = ['bank', 'nbank_autofin', 'nbank_cons', 'nbank_finlea', 'nbank_sloan', 'nbank_other']
  for (let i = 0; i < items.length; i++) {
    // 对身份证
    let item = items[i]
    const id_bad = id[item + '_bad'] == '0' ? id[item + '_bad_allnum'] : 0
    const id_overdue = id[item + '_overdue'] == '0' ? id[item + '_overdue_allnum'] : 0
    const id_lost = id[item + '_lost'] == '0' ? id[item + '_lost_allnum'] : 0
    // 对手机号
    const cell_bad = cell[item + '_bad'] == '0' ? cell[item + '_bad_allnum'] : 0
    const cell_overdue = cell[item + '_overdue'] == '0' ? cell[item + '_overdue_allnum'] : 0
    const cell_lost = cell[item + '_lost'] == '0' ? cell[item + '_lost_allnum'] : 0
    let sum = tranStr(id_bad) + tranStr(id_overdue) + tranStr(id_lost) + tranStr(cell_bad) + tranStr(cell_overdue) + tranStr(cell_lost)
    fmmd.push(sum != 0 ? '命中（' + sum + '）' : '未命中')
  }
  return fmmd
}

export function disposeLsjdxw(ApplyLoanStr) {
  let m12 = ApplyLoanStr.m12
  let lst = ApplyLoanStr.lst
  let sqcs = []
  let sqjg = []
  let fljd_params = [
    'bank',
    'nbank',
    'rel',
    'cooff',
    'caoff',
    'pdl',
    'coon',
    'caon',
    'af',
    'oth'
  ]
  for (let j = 0; j < fljd_params.length; j++) {
    let fp = fljd_params[j]
    let id = m12.id != undefined ? m12.id[fp] != undefined ? m12.id[fp] : 0 : 0
    let cell = m12.cell != undefined ? m12.cell[fp] != undefined ? m12.cell[fp] : 0 : 0
    if (id != 0 && cell != 0) {
      sqcs.push(tranStr(id.allnum) + tranStr(cell.allnum))
      sqjg.push(tranStr(id.orgnum) + tranStr(cell.orgnum))
    } else if (id != 0 && cell == 0) {
      sqcs.push(tranStr(id.allnum))
      sqjg.push(tranStr(id.orgnum))
    } else if (id == 0 && cell != 0) {
      sqcs.push(tranStr(cell.allnum))
      sqjg.push(tranStr(cell.orgnum))
    } else {
      sqcs.push(0)
      sqjg.push(0)
    }
  }

  let id_back = lst.id != undefined ? lst.id.back != undefined ? lst.id.back : 0 : 0
  let id_nback = lst.id != undefined ? lst.id.nback != undefined ? lst.id.nback : 0 : 0
  let cell_back = lst.cell != undefined ? lst.cell.back != undefined ? lst.cell.back : 0 : 0
  let cell_nback = lst.cell != undefined ? lst.cell.nback != undefined ? lst.cell.nback : 0 : 0
  id_back = tranStr(id_back)
  id_nback = tranStr(id_nback)
  cell_back = tranStr(cell_back)
  cell_nback = tranStr(cell_nback)
  let min = id_nback
  if (id_back < id_nback && id_nback != 0) {
    min = id_nback
  }
  if (cell_back < min && cell_back != 0) {
    min = cell_back
  }
  if (cell_nback < min && cell_nback != 0) {
    min = cell_nback
  }
  return {
    sqjg,
    sqcs,
    jlts: min == 0 ? '未统计到' : min
  }
}

export function getDeepData(data, keys) {
  let i = 0
  while (i < keys.length) {
    if (data[keys[i]] == undefined) {
      return '未命中'
    } else {
      data = data[keys[i]]
      i++
    }
  }
  return data == '' ? '未命中' : data
}

export function getAJnum(data) {
  if ('cases' in data) {
    return data.cases.length + '条'
  }
  return '未命中（0条）'
}
function deep(data) {
  if ('next' in data) {
    return [{
      name: data.c_ah,
      children: deep(data.next)
    }]
  } else {
    return [{
      name: data.c_ah
    }]
  }
}





